<?php
/**
 * OWL Default content class
 * This class shall print some default content such as site name, site logo...
 *
 * @package OWL
 * @subpackage Main
 * @author KeiMOon
 * @since 1.0
 */

if (!defined("DEFAULT_SITE_LOGO")) {
    define("DEFAULT_SITE_LOGO", "images/logo/owl-logo-light-96.png");
}
class Owl_Default_Content {
/**
 * Print the site's name
 */
    static function site_name() {
        echo Owl_Format::esc_html(get_option('site_name'));
    }

    /**
     * Print the site's description
     */
    static function site_description() {
        echo Owl_Format::esc_html(get_option('site_description'));
    }

    /**
     * Print the site's logo
     */
    static function site_logo($size = 96) {
        echo Owl_Html::make_it_image(Owl_Link::site_url(false) . get_option("site_logo", DEFAULT_SITE_LOGO), "Site logo", false,  $size);
    }

    /**
     * Print the default owl's logo.
     *
     * @param int $size Size of the logo, may be 96, 128, 256 or 512
     * @param bool $dark Dark or Light
     * @return bool
     */
    static function owl_logo($size = 96, $dark = false) {
        $color = ($dark) ? "dark" : "light";
        $filename = "owl-logo-" . $color . "-" . $size . ".png";
        if (!file_exists(IMAGE_PATH . "logo/" . $filename)) {
            return false;
        }
        echo Owl_Html::make_it_image(Owl_Link::image_url(false) . "logo/" . $filename, "OWL Logo");
    }

    /**
     * Print the favicon
     */
    static function site_favicon() {
        echo '<link rel="SHORTCUT ICON" href="' . Owl_Link::image_url(false) . 'logo/owl-favicon.ico"/>';
    }

    /**
     * Print the owl header, use this function pls
     */
    static function owl_head() {
        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
        echo "<meta type=\"generator\" content=\"OWL " . OWL_VERSION ."\" />";
        echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf(__("Notes from %s"), Owl_Format::esc_html(get_option('site_name'))) . '" href="' . Owl_Link::template_url("rss", false, array("var" => "notes")) . '">';
        echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf(__("Pages from %s"), Owl_Format::esc_html(get_option('site_name'))) . '" href="' . Owl_Link::template_url("rss", false, array("var" => "pages")) . '">';
        echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf(__("Documents from %s"), Owl_Format::esc_html(get_option('site_name'))) . '" href="' . Owl_Link::template_url("rss", false, array("var" => "documents")) . '">';
        print_style();
    }

    /**
     * Print the owl footer, use this function pls
     */
    static function owl_foot() {
        enqueue_script('jquery-ui');
        print_script();
    }

    /**
     * Print the default links, use this function pls
     */
    static function default_links() {
        $help = array(
            'home' => 'index.html',
            'note' => 'basic.html#post-your-note',
            'info' => 'basic.html#update-your-personal-info',
            'product' => 'basic.html#update-your-personal-info',
            'pages-list' => 'basic.html#write-your-page',
            'page' => 'basic.html#write-your-page',
            'course' => 'basic.html#courses-and-documents',
            'add-page' => 'basic.html#write-your-page',
            'documents-manager' => 'basic.html#courses-and-documents',
            'pages-manager' => 'basic.html#write-your-page',
            'edit-page' => 'basic.html#write-your-page',
            'password' => 'getting-start.html#change-your-password',
            'system-options' => 'customize.html#system-options',
            'theme-customize' => 'customize.html#themes',
            'themes-manager' => 'customize.html#themes',
            'widgets-manager' => 'customize.html#widget'
        );
        
        $user = Owl_User::get_instance();
        if ($user->is_logged_in()) {
            $data = array(
                Owl_Html::make_it_link(Owl_Link::site_url(false), Owl_Format::esc_html(get_option('site_name'), __('Home page'))),
                Owl_Html::make_it_link(Owl_Link::template_url('logout', false), __("Logout")),
                Owl_Html::make_it_link(Owl_Link::help_url(false) . $help[Owl_Functions::current_template()], __('OWL Help')),
                Owl_Html::make_it_link('https://sites.google.com/site/ponyoproject/', __('OWL Home page')),
            );
        }
        else {
            $data = array(
                Owl_Html::make_it_link(Owl_Link::site_url(false), Owl_Format::esc_html(get_option('site_name'), __('Home page'))),
                Owl_Html::make_it_link(Owl_Link::template_url('login', false), __("Login")),
                Owl_Html::make_it_link(Owl_Link::help_url(false) . $help[Owl_Functions::current_template()], __('OWL Help')),
                Owl_Html::make_it_link('https://sites.google.com/site/ponyoproject/', __('OWL Home page')),
            );
        }
        echo Owl_Html::make_it_list($data);
    }

    /**
     * Print the default footer
     */
    static function owl_default_footer() {
        enqueue_style("default-footer");
?>

<div class="content">
    <p>
        Trang web này được xây dựng dựa trên
        <a href="http://code.google.com/p/ponyo">
            OWL - Open Web framework for Lecturer
        </a>
        theo giấy phép bản quyền
        <a href="http://www.gnu.org/copyleft/gpl.html" title="GPL v3">
            GNU General Public License version 3
        </a>
    </p>
    <p>Được phát triển bởi giảng viên và sinh viên
        <a href="http://is.hut.edu.vn">
            Bộ môn Hệ thống thông tin
        </a>
        -
        <a href="http://soict.hut.edu.vn/">
            Viện công nghệ thông tin và truyền thông
        </a>
        -
        <a href="http://www.hut.edu.vn">
            Trường đại học bách khoa Hà Nội
        </a>
    </p>
</div>

<?php
    }

    /**
     * Print the site default navigation
     *
     * This function can be hooked in be using Navigation singleton.
     * For example to register a menu call "foo", instance a Navigation object and
     * call for method register_menu("site-navigation","foo")
     */
    static function site_navigation($dropdown = true) {
        enqueue_style("default-navigation");
        enqueue_script("default-navigation");

        $navigation = Owl_Navigation::get_instance();

        if ($dropdown) {
            $owl_page = new Owl_Page();
            $pages_menu = $owl_page->get_pages_menu();
            if ($pages_menu) {
                foreach($pages_menu as $nice_url=>$page_title) {
                    $navigation->register_menu("site-navigation", $nice_url, Owl_Format::trim_text($page_title, 30), Owl_Link::page_url($nice_url, false), "pages-list");
                }
                if (count($pages_menu) == get_option("max_pages_display", 5)) {
                    $navigation->register_menu("site-navigation", "more-pages", __("More..."), Owl_Link::template_url("pages-list", false), "pages-list");
                }
            }
        }

        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            $navigation->register_menu("site-navigation", "login", __("Login"), Owl_Link::template_url("login", false));
        }
        else {
            $navigation->register_menu("site-navigation", "setting", __("Setting"), Owl_Link::template_url("system-options", false));
            $navigation->register_menu("site-navigation", "logout", __("Logout"), Owl_Link::template_url("logout", false));
            if ($dropdown) {
                $navigation->register_menu("site-navigation", "add-page", __("Add page"), Owl_Link::template_url("add-page", false), 'setting');
                $navigation->register_menu("site-navigation", "pages-manager", __("Pages manager"), Owl_Link::template_url("pages-manager", false), 'setting');
                $navigation->register_menu("site-navigation", "documents-manager", __("Documents manager"), Owl_Link::template_url("documents-manager", false), 'setting');
                $navigation->register_menu("site-navigation", "system-options", __("System options"), Owl_Link::template_url("system-options", false), 'setting');
                $navigation->register_menu("site-navigation", "themes-manager", __("Themes manager"), Owl_Link::template_url("themes-manager", false), 'setting');
                $navigation->register_menu("site-navigation", "widgets-manager", __("Widgets manager"), Owl_Link::template_url("widgets-manager", false), 'setting');
                $navigation->register_menu("site-navigation", "password", __("Change password"), Owl_Link::template_url("password", false), 'setting');
            }
        }

        $navigation->register_menu("site-navigation", "report-bug", __("Bug reporting"), "#");
        $navigation->register_menu("site-navigation", "report-bug-form", self::print_bug_report_form(), false, "report-bug");
        
        
        if (Owl_Functions::current_template() == "page") {
            $current_id = "pages-list";
        }
        elseif (in_array(Owl_Functions::current_template(), array("add-page", "pages-manager", "documents-manager", "system-options", "themes-manager", "widgets-manager", "password"))) {
            $current_id = "setting";
        }
        else {
            $current_id = Owl_Functions::current_template();
        }
        $navigation->print_navigation_handle("site-navigation", $current_id);
    }

    static function setting_navigation() {
        $navigation = Owl_Navigation::get_instance();
        if (Owl_Functions::current_template() == "edit-page") {
            $current_id = "pages-manager";
        }
        elseif (Owl_Functions::current_template() == "theme-customize") {
            $current_id = "themes-manager";
        }
        else {
            $current_id = Owl_Functions::current_template();
        }
        $navigation->print_navigation_handle('setting-navigation', $current_id);
    }

    static function get_site_author() {
        $owl_personal = new Owl_Personal();
        $info = $owl_personal->get_personal();
        return Owl_Format::esc_html(__($info["name"], true));
    }
    /**
     * Print the site's author
     */
    static function site_author() {
        echo self::get_site_author();
    }

    /**
     * Print the ajax feedback content
     */
    static function print_ajax_feedback($loader = true) {
        echo "<div class=\"ajax-feedback\">\n";
        if ($loader) {
            echo "<img src=\"" . Owl_Link::image_url(false) . "default/ajax-load.gif\" class=\"ajax-load\" />\n";
        }
        echo "<div class=\"ajax-message\"></div>\n";
        echo "</div>\n";
    }

    /**
     * Print course template, use this function pls
     */
    static function print_course() {
        enqueue_script("default-course");
        enqueue_style("default-pagejson");
        enqueue_style("default-treeview");
        enqueue_style("default-ajax");
        $user = Owl_User::get_instance();
        if ($user->is_logged_in()) {
            $owl_course = new Owl_Course();
?>
<div>  

    <div class="form-container hidden">
<?php
        echo $owl_course->build_form();    
?>
    </div>
    <div id="course-add-form-controller" class="course-form-controller">
        <a href="#" class="course-add-form-toggle info-extra add"><span><?php echo __("Add"); ?></span></a>
    </div>

</div>
<?php } ?>
<div class="pagejson-pre-load">
    <img src="<?php Owl_Link::image_url() ?>default/ajax-load.gif" alt="Ajax load" />
</div>
<?php
if ($user->is_logged_in()) {
?>
<div id="course-json-admin" class="course-json"></div>
<?php } else { ?>
<div id="course-json" class="course-json"></div>
<?php } ?>
<?php
    }

    /**
     * Print note template, use this function pls
     */
    static function print_note() {
        enqueue_script("default-note");
        enqueue_style("default-pagejson");
        enqueue_style("default-note");
        enqueue_style("default-ajax");
        $owl_note = new Owl_Note();
        $last_note = $owl_note->get_last_note();
        $user = Owl_User::get_instance();
        if ($user->is_logged_in()) {
?>
<div id="note-add" class="form-container">
    <form action="<?php Owl_Link::ajax_url() ?>note-ajax.php" method="POST" id="form-note-add">
        <input type="hidden" name="action" value="add_note" />
        <div class="form-note-textarea-container">
            <textarea name="content" class="form-textarea form-note" rows="7"></textarea>
        </div>
        <div class="form-note-button-pane">
            <?php if($last_note) { ?>
            <div class="form-note-info">
                <?php echo sprintf(__("Last note at %s"), Owl_Date_Time::w3c_to_owl_date($last_note["create_date"], 'short')); ?>
            </div>
            <?php } ?>
            <div class="form-note-button-container">
                <img src="<?php Owl_Link::image_url() ?>default/ajax-load.gif" class="ajax-load" alt="Ajax load" />
                <input type="submit" name="submit" value="<?php echo __("Add note"); ?>" class="form-button note-add-ajax-submit" title="form-note-add"/>
            </div>
        </div>
        <div style="clear:both;"></div>
        <?php self::print_ajax_feedback(false); ?>
    </form>
</div>
<div class="pagejson-pre-load">
    <img src="<?php Owl_Link::image_url() ?>default/ajax-load.gif" alt="Ajax load" />
</div>
<div id="note-json-admin" class="note-json"></div>
<?php
        }
        else {
?>
<div class="pagejson-pre-load">
    <img src="<?php Owl_Link::image_url() ?>default/ajax-load.gif" alt="Ajax load" />
</div>
<div id="note-json" class="note-json"></div>
<?php
        }
    }

    /**
     * Print personal template, use this function pls
     */
    static function print_personal() {
        enqueue_script('default-info');
        enqueue_style('default-info');
        enqueue_style('default-ajax');
        $owl_personal = new Owl_Personal();
        $personal_info = $owl_personal->get_personal();
        if ($personal_info == false) {
            $owl_personal->print_error();
            return false;
        }
?>
<div id="personal-info">
     <div id="avatar-body">
    <?php if ($personal_info["avatar"]) { ?>
        <div id="avatar-container">
            <?php echo Owl_Html::make_it_image(Owl_Link::site_url(false) . $personal_info["avatar"], __("Avatar"), 300); ?>
        </div>
    <?php } ?>
     </div>
    <?php $user = Owl_User::get_instance();
          if ($user->is_logged_in()) { ?>
    <div id="avatar-form" class="form-container hidden">
    <?php $owl_personal->print_avatar_form(); ?>
    </div>
    <div id="avatar-controller" class="form-controller">
        <?php $owl_personal->print_form_control('avatar'); ?>
    </div>
    <?php } ?>
    <h3><?php echo __("Personal info"); ?></h3>
    <div id="personal-info-body">
        <?php $owl_personal->print_personal_info(); ?>
    </div>
    <?php if ($user->is_logged_in()) { ?>
    <div id="personal-info-form" class="form-container hidden">
    <?php $owl_personal->print_personal_form(); ?>
    </div>
    <div id="personal-info-controller" class="form-controller">
        <?php $owl_personal->print_form_control('personal-info'); ?>
    </div>
    <?php } ?>
</div>
<div id="academics">
    <h3><?php echo __("Academics record"); ?></h3>
    <div id="academics-body">
    <?php $owl_personal->print_academics(); ?>
    </div>
    <?php if ($user->is_logged_in()) { ?>
    <div id="academics-form" class="form-container hidden">
    <?php $owl_personal->print_academics_form(); ?>
    </div>
    <div id="academics-controller" class="form-controller">
        <?php $owl_personal->print_form_control('academics'); ?>
    </div>
    <?php } ?>
</div>
<div id="interests">
    <h3><?php echo __("Interests"); ?></h3>
    <div id="interests-body">
    <?php $owl_personal->print_interests(); ?>
    </div>
    <?php if ($user->is_logged_in()) { ?>
    <div id="interests-form" class="form-container hidden">
    <?php $owl_personal->print_interests_form(); ?>
    </div>
    <div id="interests-controller" class="form-controller">
        <?php $owl_personal->print_form_control('interests'); ?>
    </div>
    <?php } ?>
</div>
<div id="certifications">
    <h3><?php echo __("Certifications"); ?></h3>
    <div id="certifications-body">
    <?php $owl_personal->print_certifications(); ?>
    </div>
    <?php if ($user->is_logged_in()) { ?>
    <div id="certifications-form" class="form-container hidden">
    <?php $owl_personal->print_certifications_form(); ?>
    </div>
    <div id="certifications-controller" class="form-controller">
        <?php $owl_personal->print_form_control('certifications'); ?>
    </div>
    <?php } ?>
</div>
<?php
    }

    /**
     * Print products template, use this function pls
     */
    static function print_products() {
        $owl_product = new Owl_Product();
        $papers = $owl_product->get_all_products('Paper');
        $projects = $owl_product->get_all_products('Project');
        enqueue_style('default-ajax');
        enqueue_style('default-info');
        enqueue_script('default-product');
        $user = Owl_User::get_instance();
?>
<div id="papers">
    <?php $owl_product->print_papers(); ?>
</div>
<div id="projects">
    <?php $owl_product->print_projects(); ?>
</div>
<?php
    }

    /**
     * Print pages template, use this function pls
     */
    static function print_pages_list() {
        enqueue_script('default-page');
        enqueue_style('default-pages,default-pagejson,default-ajax');
?>
<div class="pagejson-pre-load">
    <img src="<?php Owl_Link::image_url() ?>default/ajax-load.gif" alt="Ajax load" />
</div>
<div id="pages-json" class="pages-json"></div>
<?php
    }

    static function print_page_content($page_data) {
        $content_file = PAGE_PATH . $page_data["content_file"];
        $page_content = (file_exists($content_file)) ? file_get_contents($content_file) : sprintf(__("Content file %s is missing"), $page_data["content_file"]);
?>
<div id="owl-page-title">
    <h2><?php echo Owl_Format::esc_html($page_data["title"]);  ?></h2>
</div>
<div id="owl-page-main">
    <div id="owl-page-content">
        <?php echo $page_content; ?>
    </div>
    <div id="owl-page-info">
        <?php
        if ($page_data["create_date"] == $page_data["modify_date"]) {
            echo "<div class=\"page-detail\">" . sprintf(__('Posted by <span class="page-author">%s</span> at <span class="page-time">%s</span>'), self::get_site_author(), Owl_Date_Time::w3c_to_owl_date($page_data["create_date"])) . "</div>";
        }
        else {
            echo "<div class=\"page-detail\">" . sprintf(__('Modified by <span class="page-author">%s</span> at <span class="page-time">%s</span>'), self::get_site_author(), Owl_Date_Time::w3c_to_owl_date($page_data["modify_date"])) . "</div>";
        }
        ?>
    </div>
</div>
<?php
    }

    static function print_bug_report_form() {
        $string = '<form action="http://keimoon.co.cc/owl-bug-report/" method="POST" style="width: 300px; background: white; border:solid 2px black; padding: 5px;">';
        $string .= Owl_Form::print_hidden(array(
            'name' => 'action',
            'value' => 'report_bug',
            'toString' => true
        ));
        $string .= Owl_Form::print_hidden(array(
            'name' => 'bug_report_author',
            'value' => self::get_site_author(),
            'toString' => true
        ));
        $string .= Owl_Form::print_hidden(array(
            'name' => 'bug_report_ip',
            'value' => Owl_Http::get_agent_ipaddress(),
            'toString' => true
        ));
        $string .= Owl_Form::print_hidden(array(
            'name' => 'bug_template',
            'value' => Owl_Functions::current_template(),
            'toString' => true
        ));
        $string .= '<p style="padding:5px 0px; font-size: 11px; font-style:italic;">' . __('Thank you for your support!') .  '</p>';
        $string .= '<div>';
        $string .= '<label style="color: #111; font-weight: bold;">' . __('Bug summary') . '</label>';
        $string .= '</div>';

        $string .= '<div>';
        $string .= Owl_Form::print_textbox(array(
            'name' => 'bug_summary',
            'toString' => true
        ));
        $string .= '</div>';

        $string .= '<div>';
        $string .= '<label style="color: #111; font-weight: bold;">' . __('Bug type') . '</label>';
        $string .= '</div>';

        $string .= '<div>';
        $string .= Owl_Form::print_combo_box(array(
            'name' => 'bug_type',
            'options' => array(
                '0' => __('Low'),
                '1' => __('Medium'),
                '2' => __('High'),
                '3' => __('Critical')
            ),
            'toString' => true
        ));
        $string .= '</div>';

        $string .= '<div>';
        $string .= '<label style="color: #111; font-weight: bold;">' . __('Bug detail') . '</label>';
        $string .= '</div>';

        $string .= '<div>';
        $string .= Owl_Form::print_textarea(array(
            'name' => 'bug_detail',
            'rows' => 3,
            'toString' => true
        ));
        $string .= '</div>';
        
        $string .= '<div>';
        $string .= '<input type="submit" name="submit" value="' . __('Report') . '" style="padding: 5px 10px; background: white; border: solid 1px #333; cursor: pointer;" />';
        $string .= '</div>';
        $string .= '</form>';
        return $string;
    }
}


